﻿using System;
using System.Collections.Generic;
using s2google.sync.SyncProviders;
using sp2google.common;
using sp2google.common.Entities.Calendar;
using sp2google.common.Interfaces;

namespace s2google.sync
{
    public class CalendarSync
    {
        public Configuration Config { get; protected set; }

        public CalendarSync(Configuration config)
        {
            Config = config;
        }

        public void Sync(SyncProvider from, SyncProvider to)
        {
            if (from == to) return;

            try
            {
                //using (var source = GetSyncer(from))
                using (var target = GetSyncer(to))
                {
                    //var items = source.GetCollection();

                    var items = new List<CalendarItem>();
                    items = (List<CalendarItem>)Utils.XmlDeserialize(@"../../data/calendar_items.xml", items.GetType());

                    foreach (var item in items) //.Where(item => !target.Exists(item)))
                        target.Add(item);
                }
            }
            catch (Exception ex)
            {
                
            }
        }

        private ISyncProvider<CalendarItem> GetSyncer(SyncProvider provider)
        {
            ISyncProvider<CalendarItem> syncer;

            //if a new SyncProvider is added, this is the
            //only part that needs to be changed

            switch (provider)
            {
                case SyncProvider.SharePoint:
                    syncer = new SharePointProvider();
                    break;
                case SyncProvider.Google:
                    syncer = new GoogleProvider();
                    break;
                default: throw new ArgumentOutOfRangeException("provider");
            }

            syncer.Init(Config);

            return syncer;
        }
    }
}
